package com.example.custom_bottom_view.CustomBottomView

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.BounceInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.drawable.DrawableCompat
import com.example.custom_bottom_view.R
import com.google.android.material.internal.ViewUtils.dpToPx

class TabIconView(context: Context, attrs: AttributeSet) : ViewGroup(context, attrs) {

    var index: Int = 0
    private lateinit var mIcon: Drawable
    private lateinit var mTitle: String
    private var mNormalColor: Int = 0
    private var mSelectedColor: Int = 0

    private lateinit var mIconView: ImageView
    private lateinit var mTitleView: TextView

    var addListener: ((TabIconView, Int) -> Unit)? = null

    @SuppressLint("RestrictedApi")
    private val mIconSize = dpToPx(context, 24).toInt()
    private val mTitleSize = 14f
    private val mSpace = 5f

    init {
        parseAttrs(attrs)
        addChild()
        setOnClickListener {
            touchEvent()
        }
    }

    private fun touchEvent() {
        val scale = AnimationUtils.loadAnimation(context, R.anim.scale).apply {
            interpolator = BounceInterpolator()
        }
        mIconView.startAnimation(scale)
        mTitleView.startAnimation(scale)

        addListener?.let { it(this, index) }

    }

    fun isSelect(isSelected: Boolean) {
        val currentColor = if (isSelected) mSelectedColor else mNormalColor
        mIconView.setImageDrawable(tintDrawable(mIcon, currentColor))
        mTitleView.setTextColor(currentColor)
    }

    private fun tintDrawable(drawable: Drawable, color: Int): Drawable {
        val tintDrawable = DrawableCompat.wrap(drawable)
        DrawableCompat.setTint(drawable, color)
        return tintDrawable
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun parseAttrs(attrs: AttributeSet) {
        context.obtainStyledAttributes(attrs, R.styleable.TabIconView).apply {
            mIcon = getDrawable(R.styleable.TabIconView_mIcon)
                ?: resources.getDrawable(R.drawable.ic_launcher_foreground)
            mTitle = getString(R.styleable.TabIconView_title).toString()
            mNormalColor = getColor(R.styleable.TabIconView_normal_color, 0)
            mSelectedColor = getColor(R.styleable.TabIconView_selected_color, 0)
            recycle()
        }
    }

    private fun addChild() {
        mIconView = ImageView(context).apply {
            setImageDrawable(mIcon)
            scaleType = ImageView.ScaleType.FIT_CENTER
        }
        mTitleView = TextView(context).apply {
            text = mTitle
            setTextSize(TypedValue.COMPLEX_UNIT_SP, mTitleSize)
            setTextColor(mNormalColor)
        }
        addView(mIconView, mIconSize, mIconSize)
        addView(mTitleView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

        measureChild(mTitleView, MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
    }


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val mIconLeft = (width - mIconSize) / 2
        val mIconTop = ((height - mIconSize - mSpace - mTitleView.measuredHeight) / 2).toInt()
        mIconView.layout(mIconLeft, mIconTop, mIconLeft + mIconSize, mIconTop + mIconSize)

        val mTitleLeft = (width - mTitleView.measuredWidth) / 2
        val mTitleTop = (mIconTop + mIconSize + mSpace).toInt()
        mTitleView.layout(
            mTitleLeft,
            mTitleTop,
            mTitleLeft + mTitleView.measuredWidth,
            (mTitleTop + mTitleView.measuredHeight)
        )
    }
}